﻿/*****************************************************************************/
//Jquery validate zh-cn
$().ready(function () {
    jQuery.extend(jQuery.validator.messages, {
        required: "此项必填",
        remote: "请修正此项",
        email: "请输入一个合法的电子邮件地址",
        url: "请输入合法网址",
        date: "请输入合法日期",
        dateISO: "请输入合法日期（ISO格式）",
        number: "请输入合法数字",
        digits: "请只输入数字",
        creditcard: "请输入一个有效的信用卡号",
        equalTo: "您两次输入的密码不一致",
        maxlength: jQuery.validator.format("请输入不超过{0}个字符"),
        minlength: jQuery.validator.format("请输入至少{0}个字符"),
        rangelength: jQuery.validator.format("请输入{0}-{1}位字符"),
        range: jQuery.validator.format("请输入介于{0}和{1}的值"),
        max: jQuery.validator.format("请输入一个小于或等于{0}的值"),
        min: jQuery.validator.format("请输入一个大于或等于{0}的值")
    });
});


/*****************************************************************************/
//Jquery validate extends
(function () {
    function stripHtml(value) {
        // remove html tags and space chars
        return value.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' ')
		// remove punctuation
		.replace(/[.(),;:!?%#$'"_+=\/\-]*/g, '');
    }
    jQuery.validator.addMethod("maxWords", function (value, element, params) {
        return this.optional(element) || stripHtml(value).match(/\b\w+\b/g).length <= params;
    }, jQuery.validator.format("Please enter {0} words or less."));

    jQuery.validator.addMethod("minWords", function (value, element, params) {
        return this.optional(element) ||value.length >= params;
    }, jQuery.validator.format("至少输入 {0} 个字符."));

    jQuery.validator.addMethod("rangeWords", function (value, element, params) {
        var valueStripped = stripHtml(value);
        var regex = /\b\w+\b/g;
        return this.optional(element) || valueStripped.match(regex).length >= params[0] && valueStripped.match(regex).length <= params[1];
    }, jQuery.validator.format("Please enter between {0} and {1} words."));

    jQuery.validator.addMethod("integer", function (value, element) {
        return this.optional(element) || /^-?\d+$/.test(value);
    }, "A positive or negative non-decimal number please");

    jQuery.validator.addMethod("digitalOrChar", function (value, element) {
        return this.optional(element) || /^([0-9]|[A-Za-z])+$/.test(value);
    }, "请输入字母和数字的组合，不能包含特殊字符和空格");

    //正整数
    jQuery.validator.addMethod("positiveInteger", function (value, element) {
        return this.optional(element) || /^\d+$/.test(value);
    }, "请输入一个正确整数");

    //中国大陆身份证号码
    jQuery.validator.addMethod("isIdCardNo", function (value, element) {
        return this.optional(element) || /(^\d{15}$)|(^\d{17}(\d|X)$)/.test(value);
    }, "请输入正确的身份证号码");

    //中国大陆手机号码
    jQuery.validator.addMethod("mobilePhoneNumber", function (value, element) {
        return this.optional(element) || /^(((1[0-9]{2}))+\d{8})$/.test(value);
    }, "请输入正确的手机号码");

    jQuery.validator.addMethod("cardNum", function (value, element) {
        return this.optional(element) || /^\d+$/.test(value);
    }, "请输入正确的手机号码");

    jQuery.validator.addMethod("mt4Account", function (value, element) {
        return this.optional(element) || /^\d+$/.test(value);
    }, "请输入正确的经纪商账户");

    jQuery.validator.addMethod("nickNamereg", function (value, element) {
        return this.optional(element) || /^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(value);
    }, "请输入合法的昵称");
    
    jQuery.validator.addMethod("isInt", function (value, element) {
        return this.optional(element) || /^\d+$/.test(value);
    }, "只能输入数字");

    jQuery.validator.addMethod("isdigitalOrChar", function (value, element) {
        return this.optional(element) || /^([0-9]|[A-Za-z])+$/.test(value);
    }, "请输入字母和数字的组合");
}());